package com.yami.shop.personnft.common.listener;

import com.yami.shop.bean.model.NftTransferRecord;
import com.yami.shop.common.bean.MidMnemonic;
import com.yami.shop.common.config.RocketMqConstant;
import com.yami.shop.common.util.Json;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.personnft.common.service.NftTransferRecordService;
import com.yami.shop.personnft.common.service.PersonNftService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * 转入商城
 * @author FrozenWatermelon
 * @date 2021/1/7
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.NFT_Into_SHIFT,consumerGroup = RocketMqConstant.NFT_Into_SHIFT)
@Slf4j
public class NftIntoConsumer implements RocketMQListener<Map<String,String>> {

    @Autowired
    private RocketMQTemplate stockMqTemplate;
    @Autowired
    private PersonNftService personNftService;
    @Autowired
    private ShopConfig shopConfig;
    
    @Autowired
    private NftTransferRecordService nftTransferRecordService;

    /**
     * 二级市场托管户转入商城
     * @param map
     */
    @SneakyThrows
    @Override
    public void onMessage(Map<String,String> map) {
        log.info("nft 中间库转入到指定链上地址... message: " + Json.toJsonString(map));
        String denomId = map.get("denomId");
        //tokenid
        String nftId = map.get("nftId");
        //指定链上地址
        String reci = map.get("reci");
        String userId = map.get("userId");
        MidMnemonic midMnemonic = shopConfig.getMidMnemonic();//转出人的助计词
        String mnemonic = midMnemonic.getMnemonic();
        String sendReci = midMnemonic.getReci();
        if(ObjectUtils.isEmpty(denomId)||ObjectUtils.isEmpty(mnemonic)||ObjectUtils.isEmpty(sendReci) ||ObjectUtils.isEmpty(nftId)){
            log.info("nft 中间库转入到指定链上地址库 参数不足... message:reci: "+reci+"denomId :"+denomId+"nftId:"+nftId);
            return;
        }

        NftTransferRecord record = new NftTransferRecord();
        record.setDenomId(denomId);
        record.setNftId(nftId);
        record.setReci(reci);
        record.setUserId(userId);
        record.setMnemonic(mnemonic);
        record.setSenderAddress(sendReci);
        record.setType(1);
        nftTransferRecordService.save(record);
        log.info("NFT转入 已保存到待划转【toReci:" + reci + "denomId:" +denomId+ "nftId:" + nftId + "】");
    }
}
